import time

from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait

from model.buy_page import BuyPage

"""
    高防云购买页
    
"""


class HighDefCloudBuyPage(BuyPage):
    type: str
    line: str
    cpu: str
    mem: str
    bandwidth: str
    dataDisk: []
    defPeak: str
    osName: str
    osVersion: str
    buyNum: str

    def __init__(self, wd: WebDriver):
        super().__init__(wd)
        self.type = "高防云"
    """
        点击选择一个线路 (地址)
        0.华东高防2[泉州电信]
        1.华东高防1[浙江电信]
        2.福建企业[厦门电信]
    """

    def ClickLine(self, num: int):
        # 定位到cpu的父元素
        button = self.SelectButton("//*[@id=\"address\"]", "a|div", num)
        self.line = button.text

    """
    
        点击选择 CPU 的核心数
    """

    def ClickCPU(self, num: int):
        select_button = self.SelectButton("//*[@id=\"cpuSelect\"]", "div", num)
        self.cpu = select_button.text

    """
        选择内存数
        num: int 位置
    
    """

    def ClickMem(self, num: int):
        button = self.SelectButton("//*[@id=\"ramSelect\"]", "div", num)
        self.mem = button.text

    """
    
            设置默认数据盘
            //*[@id="ssdAdd"]/div/div[2]/div/span[1]/input
    """
    def SetDefaultDataDisk(self, size: int):
        # //*[@id="ssdAdd2"]/div/div[2]/div/span[1]/input
        # //*[@id="ssdAdd"]/div/div[2]/div/span[1]/input
        inputDataDisk = WebDriverWait(self.wd, 10, 0.5) \
            .until(lambda bb: self.wd.find_element_by_xpath("//*[@id=\"ssdAdd\"]/div/div[2]/div/span[1]/input"))
        self.InputClear(inputDataDisk)
        inputDataDisk.send_keys(size)

    def AddDisk(self, num, sizeArr: [int]):
        temp = 1
        while temp < num:
            xpath = self.wd.find_element_by_xpath("//*[@id=\"addDisk\"]")
            if xpath is not None:
                temp += 1
                xpath.click()
        dataDisks = self.wd.find_elements_by_xpath("//*[contains(@id,'ssdAdd')]")
        k = 0

        for k, v in enumerate(dataDisks):
            # //*[@id="ssdAdd2"]/div/div[2]/div/span[1]/input
            # /html/body/div[6]/div[2]/div/form/div/div[1]/div[2]/div[6]/div[2]/div/div[2]/div/span[1]/input
            inp = v.find_element_by_xpath("./[@name=\"harddisks[]\"]")
            inp.send_keys(sizeArr[k])
            k += 1
        # //*[@id="ssdAdd2"]/div/div[2]/div/span[1]/input
        # //*[@id="ssdAdd4"]/div/div[2]/div/span[1]/input
        # //*[@id="ssdAdd5"]/div/div[2]/div/span[1]/input

    """
        设置带宽数值
    """

    def SetBandwidth(self, num: int):
        # // *[ @ id = "bandwidthBox"] / span[1] / input
        self.SelectInput("//*[@id=\"bandwidthBox\"]/span[1]/input", str(num))

    """
        设置防御带宽
    """

    def SetDefPeak(self, pos: int):
        self.SelectButton("//*[@id=\"bidders\"]", "div", pos)

    """
        设置系统镜像
    """

    def SetOsImg(self, imgName: str):
        img = self.wd.find_element_by_class_name(imgName)
        self.wd.execute_script("arguments[0].click();", img)

    """
        设置系统镜像的版本
    """

    def SetOsImgVersion(self, pos: int):
        self.SelectButton("//*[@id=\"versionList\"]", "p", pos)

    """
        设置 系统镜像以及版本
    """

    def SetOsImgAndVersion(self, imageName: str, pos: int):
        self.SetOsImg(imageName)
        self.SetOsImgVersion(pos)

    """
        设置自动续费, 默认为 否
    """

    def SetAutoRenew(self, isRenew=False):
        buttonPos = "//*[@id=\"renew_no\"]"
        if isRenew:
            buttonPos = "//*[@id=\"renew_yes\"]"
        xpath = self.wd.find_element_by_xpath(buttonPos)
        xpath.click()

    """
        设置云服务器台数
    """

    def SetCloudNumber(self, num: int):
        self.SelectInput("//*[@id=\"serverNum\"]/span[1]/input", str(num))

    """
    
        点击立即购买
         
    """

    def ClickBuyButton(self):
        xpath = self.wd.find_element_by_xpath("//*[@id=\"forthwithBtn\"]")
        xpath.click()

    """
        点击购买时长
    """

    def ClickBuyLength(self, pos):
        # 因为取到的第一个数据是 错误的, 直接+1 过滤掉.
        self.SelectButton("//*[@id=\"serverTimes\"]/div", "div", pos + 1)
